import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;

/*
测试用例
 */
public class Demo {
    /*
    查询所有
     */
    @Test
    public void selectAll() throws Exception {
        //获取sqlSessionFactory对象工厂
        String resource = "mybatis-config.xml"; //加载核心配置文件
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqf = new SqlSessionFactoryBuilder().build(inputStream);

        //获取SqlSession对象执行sql
        SqlSession son = sqf.openSession();

        //获取映射文件接口字节码文件
        BrandMapper mapper = son.getMapper(BrandMapper.class);

        //执行方法
        List<Brand> brands = mapper.selectAll();
        System.out.println(brands);

//        //释放资源
//        son.close();
    }


    /*
    单条件查询
     */
    @Test
    public void selectById() throws Exception {
        //模拟接收用户反馈查询的id
        int id = 1;
        //获取sqlSessionFactory对象工厂
        String resource = "mybatis-config.xml"; //加载核心配置文件
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqf = new SqlSessionFactoryBuilder().build(inputStream);
        //获取sqlSession对象
        SqlSession son = sqf.openSession();
        //获取映射文件的Class字节码文件
        BrandMapper mapper = son.getMapper(BrandMapper.class);
        //调用方法
        List<Brand> brands = mapper.selectById(id);
        System.out.println(brands);
        //释放资源
        son.close();
    }


    /*
    多条件查询
     */
    @Test
    public void selectTb() throws Exception {
        //模拟用户接收数据
        int s = 1;
        String n = "小米";

        //因为是模糊查询所以拼接一下字符串   sql注入的方法
//        String nMax="%"+n+"%";

        //获取SQlSessionFactory对象工厂
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqf = new SqlSessionFactoryBuilder().build(inputStream);

        //获取SqlSession对象
        SqlSession son = sqf.openSession();

        //获取映射接口的字节码文件
        BrandMapper mapper = son.getMapper(BrandMapper.class);

        //调用方法
        List<Brand> brands = mapper.selectTb(s, n);

        //查看返回值
        System.out.println(brands);

        //释放资源
        son.close();
    }

    /*
    动态sql多条件查询
     */
    @Test
    public void seectDt() throws Exception {
        //模拟用户接收数据
        int s = 1;
        String c = "华为";
        String b = " 华为";

        //模糊查询
        c = "%" + c + "%";
        b = "%" + b + "%";

        //把值封装到Map集合中
        HashMap map = new HashMap();  //创建Map集合
        map.put("status", s);
        map.put("companyName", c);
        map.put("brandName", b);

        //获取SQlSessionFactory对象工厂
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqf = new SqlSessionFactoryBuilder().build(inputStream);

        //获取SqlSession对象
        SqlSession son = sqf.openSession();

        //获取映射接口的字节码文件
        BrandMapper mapper = son.getMapper(BrandMapper.class);

        //调用方法
        List<Brand> brands = mapper.selectDt(map);
        System.out.println(brands);

        //释放资源
        son.close();
    }

    /*
    新增用户
      */
    @Test
    public void insertAdd() throws Exception {
        //模拟用户输入数据
        String brandName = "腾讯";
        String companyName = "腾讯科技有限责任公司";
        int ordered = 88;
        String description = "充钱使你强大";
        int status = 1;

        //封装对象
        Brand brand = new Brand();
        brand.setBrandName(brandName);
        brand.setCompanyName(companyName);
        brand.setOrdered(ordered);
        brand.setDescription(description);
        brand.setStatus(status);

        //获取SQlSessionFactory对象工厂
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqf = new SqlSessionFactoryBuilder().build(inputStream);

        //获取Session对象执行方法
        SqlSession son = sqf.openSession();

        //获取映射文件接口的字节码文件
        BrandMapper mapper = son.getMapper(BrandMapper.class);

        //执行方法
        int x = mapper.insertAdd(brand);//把封装进Brand的参数传过去
        son.commit();
        System.out.println(x);
        //释放资源
        son.close();
    }

}